package webdev.danagtravel;

import java.util.ArrayList;

import webdev.adapters.PlaceDetailAdapter;
import webdev.common.Common;
import webdev.database.SQLQuery;
import webdev.dto.CommentDTO;
import webdev.dto.DiaDiemDTO;
import webdev.dto.UserDTO;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import dev.danang.R;

/**
 * @author TienLQ
 */
public class PlaceDetailActivity extends Activity implements OnClickListener, OnTouchListener {
	
	public static final String TAG = PlaceDetailActivity.class.getSimpleName();
	
	private Context mContext;
	
	private DiaDiemDTO diaDiem;
	private DiaDiemDTO diaDiemDTO;
	
	UserDTO us;
	
	private ListView mListView;
	
	private TextView mTxtHeaderTitle;
	
	private ImageView mImgBack;
	
	private EditText mEdtContentComment;
	
	private RelativeLayout mLayoutMain;
	
	private ImageButton mBtnPostComment;
	
	private Button mBtnShare;
	private Button mBtnBooking;
	private Button mBtnMap;
	
	private SQLQuery sqlQuery;
	
	private ArrayList<CommentDTO> comments;
	
	
	// Adapter
	PlaceDetailAdapter mPlaceDetailAdapter;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_place);
		this.mContext = getApplicationContext();
		init();
		setEventForMembers();
		setValueForMembers();
	}
	
	/**
	 * Initialize variables
	 */
	public void init() {
		Bundle args = this.getIntent().getExtras();
		diaDiem = args.getParcelable("places");

		// List view
		mListView = (ListView) findViewById(R.id.list_content_detail);
		
		// Initialize adapter
		mPlaceDetailAdapter = new PlaceDetailAdapter(getApplicationContext());
		
		// Text View
		mTxtHeaderTitle = (TextView) findViewById(R.id.text_view_header_title);
		// ImageView
		mImgBack = (ImageView) findViewById(R.id.img_back);
		// Edit text
		mEdtContentComment = (EditText) findViewById(R.id.edt_write_comment_of_article_detail);
		// Layout
		mLayoutMain = (RelativeLayout) findViewById(R.id.layout_main_of_places_detail_new);
		// Button
		mBtnPostComment = (ImageButton) findViewById(R.id.btn_post_comment_of_comment_article_detail);
		mBtnPostComment.setEnabled(false);
		
		// Button
		mBtnShare = (Button) findViewById(R.id.btn_share_detail);
		mBtnBooking = (Button) findViewById(R.id.btn_booking_detail);
		mBtnMap = (Button) findViewById(R.id.btn_map_detail);
		
		sqlQuery = new SQLQuery(getApplicationContext());
		us = new UserDTO();
		us = sqlQuery.getAllUser().get(0);
		
		comments = new ArrayList<CommentDTO>();
	}
	
	/**
	 * pass value for variables
	 */
	protected void setValueForMembers() {
		mPlaceDetailAdapter.addContentPlace(diaDiem);
		try {
			comments = sqlQuery.getAllCommentOfUser4Place(us.getmUserName(), String.valueOf(diaDiem.getId()));
			int size = comments.size();
			for(int i = 0 ; i < size ; i++) {
				mPlaceDetailAdapter.addListComment(comments.get(i));	
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		mListView.setAdapter(mPlaceDetailAdapter);
		mTxtHeaderTitle.setText(diaDiem.getTitle());
	}
	
	
	/**
	 * set event for variables
	 */
	protected void setEventForMembers() {
		mImgBack.setOnClickListener(this);
		mLayoutMain.setOnTouchListener(this);
		mListView.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				hideKeyBoard();
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
			}
		});
		
		// Event for editText call to checkData Function
		mEdtContentComment.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void afterTextChanged(Editable s) {
				checkData();
			}
		});
		
		mBtnPostComment.setOnClickListener(this);
		
		mBtnShare.setOnClickListener(this);
		mBtnBooking.setOnClickListener(this);
		mBtnMap.setOnClickListener(this);
	}
	
	/**
	 * If exits data, set enable for button post
	 */
	private void checkData() {
		if ("".equals(mEdtContentComment.getText().toString().trim())) {
			mBtnPostComment.setEnabled(false);
		} else {
			mBtnPostComment.setEnabled(true);
		}
	}
	
	@Override
	public void onClick(View v) {
	    switch (v.getId()) {
		case R.id.img_back:
			onBackPressed();
			break;
		case R.id.btn_post_comment_of_comment_article_detail:
			diaDiemDTO = new DiaDiemDTO();
			diaDiemDTO.addComment(String.valueOf(diaDiem.getId()), us.getmUserName(), mEdtContentComment.getText().toString());
			mPlaceDetailAdapter.addListComment(diaDiemDTO.getComments().get(0));
			mPlaceDetailAdapter.notifyDataSetChanged();
			mEdtContentComment.clearFocus();
			addComment();
			// Kết thúc add comment vào database
			break;
		case R.id.btn_share_detail:
			break;
		case R.id.btn_booking_detail:
			Intent goToBooking = new Intent(PlaceDetailActivity.this,BookingActivity.class);
			Bundle b = new Bundle();
			b.putParcelable("diadiem", diaDiem);
			goToBooking.putExtras(b);
			startActivity(goToBooking);
			break;
		case R.id.btn_map_detail:
			Intent goToMapDetail = new Intent(PlaceDetailActivity.this,MapDetail.class);
			Bundle bd = new Bundle();
			bd.putParcelable("diadiem", diaDiem);
			goToMapDetail.putExtras(bd);
			startActivity(goToMapDetail);
			break;
		default:
			break;
		}
	}

	private void addComment() {
		CommentDTO cm = new CommentDTO();
		cm.setContent(mEdtContentComment.getText().toString());
		cm.setUserName(Common.currentUser.getmUserName());
		cm.setNamePlace(diaDiem.getTitle());
		new SQLQuery(this).addComment(cm);
	}
	
    /**
	 * hide keyboard
	 */
    @Override
	public boolean onTouch(View v, MotionEvent event) {
		if(v == mLayoutMain) {
			hideKeyBoard();
			return true;
		}
		return false;
	}
    
    public void showKeyBoard() {
    	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    	imm.showSoftInput(mEdtContentComment, InputMethodManager.SHOW_IMPLICIT);
    }
    public void hideKeyBoard() {
    	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(mEdtContentComment.getWindowToken(), 0);
    }
    
}